package com.thy.controller;

import com.thy.common.MessageTemplate;
import com.thy.common.Result;
import com.thy.config.WechatConf;
import com.thy.pojo.U;
import com.thy.pojo.User;
import com.thy.service.UService;
import com.thy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author  MrDong
 * @create  2021/5/18 16:36
 * @desc    用户
 **/
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private WechatConf wechatConf;

    MessageTemplate messageTemplate = new MessageTemplate();

    @RequestMapping("/test")
    @Async
    public Object sendMess(){

        //所有open_id
        List<User> userList=userService.getUser();
        userList.forEach(item->{
            //userService.updateUserById(item.getId());
            // 设置模板id
            messageTemplate.setTemplateId("6LYthBPG_qIbjyd-mIxVCD_OKSdddQIXzsbzjvVQxfc");
            // 设置接收用户openId
            messageTemplate.setToUser(item.getManager().getOpenId());
            //点击详情跳转的地址
            messageTemplate.setUrl("http://www.baidu.com");

            //设置模板dada参数
            messageTemplate.getData().put("first", MessageTemplate.initData("停水用户信息,请注意查收！\n", "#79C5C3"));
            messageTemplate.getData().put("username", MessageTemplate.initData(item.getUserName(), "#00CD00"));
            messageTemplate.getData().put("sex", MessageTemplate.initData(item.getUserSex(), "#00CD00"));
            messageTemplate.getData().put("age", MessageTemplate.initData(item.getUserAge().toString(), "#00CD00"));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            messageTemplate.getData().put("time", MessageTemplate.initData(simpleDateFormat.format(new Date())+"\n", "#79C5C3"));
            messageTemplate.getData().put("water", MessageTemplate.initData(item.getWater().getWaterNum().toString(), "#00CD00"));
            messageTemplate.getData().put("electric", MessageTemplate.initData(item.getElectric().getElectricNum().toString(), "#00CD00"));
            messageTemplate.getData().put("gas", MessageTemplate.initData(item.getGas().getGasNum().toString(), "#00CD00"));
            messageTemplate.getData().put("card", MessageTemplate.initData(item.getUserCard(), "#00CD00"));
            messageTemplate.getData().put("jinji", MessageTemplate.initData(item.getEmergencyName(), "#00CD00"));
            messageTemplate.getData().put("jinjiphne", MessageTemplate.initData(item.getEmergencyPhone(), "#00CD00"));
            messageTemplate.getData().put("region", MessageTemplate.initData(item.getManager().getCommunity().getStreet().getRegion().getRegionName(), "#00CD00"));
            messageTemplate.getData().put("street", MessageTemplate.initData(item.getManager().getCommunity().getStreet().getStreetName(), "#00CD00"));
            messageTemplate.getData().put("community", MessageTemplate.initData(item.getManager().getCommunity().getCommunityName(), "#00CD00"));
            messageTemplate.getData().put("address", MessageTemplate.initData(item.getUserAddress(), "#00CD00"));
            messageTemplate.getData().put("remark", MessageTemplate.initData("点击详情可查看信息", "#79C5C3"));

            Result result = restTemplate.postForObject(String.format(WechatConf.PUSH_MESSAGE_URL, wechatConf.getAccessToken()),
                    messageTemplate, Result.class);
        });
        //调用微信接口，发送模板消息

        return null;
    }

}
